package com.gmail.drheiniger.book.organizer;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.gmail.drheiniger.backend.Book;
import com.gmail.drheiniger.backend.Database;

public class SearchBooksGUI implements ActionListener {

	// constants
	private static final String TITLE_LABEL_TEXT = "SEARCH";
	private static final String BOOK_TITLE_LABEL_TEXT = "Title:";
	private static final String AUTHOR_TITLE_LABEL_TEXT = "Author:";
	private static final String YEAR_READ_LABEL_TEXT = "Year Read";
	private static final String RATING_LABEL_TEXT = "Rating:";
	private static final String CONTENT_LABEL_TEXT = "Content:";
	private static final String SEARCH_BUTTON_TEXT = "Search";
	private static final String BACK_BUTTON_TEXT = "Back";

	// initialize components
	private JFrame frame;
	private JLabel titleLabel = new JLabel(TITLE_LABEL_TEXT);
	private JLabel bookTitleLabel = new JLabel(BOOK_TITLE_LABEL_TEXT);
	private JTextField bookTitleTextField = new JTextField();
	private JLabel authorLabel = new JLabel(AUTHOR_TITLE_LABEL_TEXT);
	private JTextField authorTextField = new JTextField();
	private JLabel yearReadLabel = new JLabel(YEAR_READ_LABEL_TEXT);
	private JTextField yearReadTextField = new JTextField();
	private JLabel ratingLabel = new JLabel(RATING_LABEL_TEXT);
	private JTextField ratingTextField = new JTextField();
	private JLabel contentLabel = new JLabel(CONTENT_LABEL_TEXT);
	private JTextField contentTextField = new JTextField();
	private JButton searchButton = new JButton(SEARCH_BUTTON_TEXT);
	private JButton backButton = new JButton(BACK_BUTTON_TEXT);

	public SearchBooksGUI() {

		frame = new JFrame("Search");
		Container contentPane = frame.getContentPane();
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		contentPane.setLayout(layout);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		titleLabel.setFont(new Font("Dialog", 1, 18));
		titleLabel.setOpaque(true);
		// titleLabel.setBackground(Color.BLACK);
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		titleLabel.setBorder(BorderFactory.createRaisedBevelBorder());

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weighty = 0.5;
		c.ipadx = 80;
		// c.ipady = 120;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		contentPane.add(titleLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 1;
		// c.weightx = .5;
		// c.weighty = .5;
		// c.ipady = 0;
		// c.ipadx = 0;
		c.ipadx = 80;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(bookTitleLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 1;
		contentPane.add(bookTitleTextField, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 2;
		contentPane.add(authorLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 2;
		contentPane.add(authorTextField, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 3;
		contentPane.add(yearReadLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 3;
		contentPane.add(yearReadTextField, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 4;
		contentPane.add(ratingLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 4;
		contentPane.add(ratingTextField, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 5;
		contentPane.add(contentLabel, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 5;
		contentPane.add(contentTextField, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 6;
		contentPane.add(searchButton, c);

		// c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 1;
		c.gridy = 6;
		contentPane.add(backButton, c);

		searchButton.addActionListener(this);
		backButton.addActionListener(this);

		// contentPane.;
		// // find the screen size
		// Toolkit toolkit = Toolkit.getDefaultToolkit();
		// Dimension screenSize = toolkit.getScreenSize();
		// // calculate center of screen
		// int x = (screenSize.width - contentPane.getWidth()) / 2;
		// int y = (screenSize.height - contentPane.getHeight()) / 2;

		this.frame.setSize(400, 500);
		this.frame.setLocationRelativeTo(null);
		this.frame.setResizable(false);
		this.frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		Component source = null;
		String srcAction = "";
		source = (JButton) (e.getSource());
		srcAction = ((JButton) source).getText();
		switch (srcAction) {
		case SEARCH_BUTTON_TEXT:
			// do delete stuff
			Database db = new Database();
			String titleString = bookTitleTextField.getText();
			String authorString = authorTextField.getText();
			String yearString = yearReadTextField.getText();
			String ratingString = ratingTextField.getText();
			String contentString = contentTextField.getText();

			Book book = new Book(titleString, authorString, yearString, ratingString, contentString);
			db.setPrepareStatement(book);
			new BrowseGUI(db);
			frame.dispose();
			break;

		case BACK_BUTTON_TEXT:
			new OverviewGUI();
			frame.dispose();
			break;

		}
	}
}